package com.clc.controller.main;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clc.service.IWeatherService;

@Controller
@RequestMapping("/main")
public class PageHomeController {
	static Log log = LogFactory.getLog(PageHomeController.class.getName());

	@Autowired
	public IWeatherService weatherService;

	@RequestMapping(value = "/pageHome")
	public String excuteAction(Model model,HttpServletRequest request) throws Exception {
		File file = new File("../cad");
		System.out.println("*************"+file.getAbsolutePath());
		log.info("PageHomeController");
		Object userName = SecurityContextHolder.getContext()
				.getAuthentication().getName();
		String[] weeks = new String[3];
		String pm25="";
		String aqiInfo="";
		/*获取最近三天的星期值*/
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
		weeks[0] = dateFm.format(calendar.getTime());
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)+1);
		weeks[1] = dateFm.format(calendar.getTime());
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)+1);
		weeks[2] = dateFm.format(calendar.getTime());
		//获取pm_2.5
		try {
			pm25 = weatherService.getLocalAQI();
			log.info("============pm25:"+pm25);
			if(pm25.startsWith("{")){
				JSONObject js = new JSONObject(pm25);
				aqiInfo = js.getString("error");
			}else{
				JSONArray jsonArray = new JSONArray(pm25);
				JSONObject pm_25 = new JSONObject(jsonArray.get(0).toString());
				aqiInfo = pm_25.getString("aqi")+"-("+pm_25.getString("quality")+")";
			}
		} catch (Exception e) {
			aqiInfo="加载pm2.5失败";
			log.info(aqiInfo);
		}
		model.addAttribute("pm25", aqiInfo);
		model.addAttribute("userName", userName);
		model.addAttribute("week", weeks);
		return "../../index";
	}

	@RequestMapping(value = "/getLocalWeather", method = RequestMethod.GET)
	public @ResponseBody
	String getLocalWeather() throws Exception {
		try {
			log.info("getLocalWeather");
			long stime = System.currentTimeMillis();
			String weaterInfo = weatherService.getLocalWeather();
			log.info("weaterInfo " + weaterInfo);
			long etime = System.currentTimeMillis();
			log.info("getLocalWeather is time " + (etime - stime));
			return weaterInfo;
		} catch (Exception e) {
			log.info("getLocalWeather load error");
		}
		return "";
	}
}
